package com.zyc.webtest.controller;

import com.zyc.webtest.domain.SysRoleEntity;
import com.zyc.webtest.service.SysRoleService;
import com.zyc.webtest.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 角色
 * 
 * @author zyc
 * @date 2017-05-02 10:20:16
 */
@Controller
@RequestMapping("sysrole")
public class SysRoleController extends BaseController{

	@Autowired
	private SysRoleService sysRoleService;
	
	/**
	 * 列表
	 */
	@GetMapping
	public String list(@RequestParam Map<String, Object> params){

		return "sysrole/list";
	}
	
	
	/**
	 * 信息
	 */
	@GetMapping("/{roleId}")
	public String info(@PathVariable("roleId") Long roleId){
		SysRoleEntity sysRoleEntity = sysRoleService.findById(roleId).orElse(new SysRoleEntity());
        return "sysrole/info/"+roleId;
	}
	
	/**
	 * 保存
	 */
	@PostMapping
    @ResponseBody
	public ResultMsg save(@RequestBody SysRoleEntity sysRole){
        return sysRoleService.save(sysRole).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
	}
	
	/**
	 * 修改
	 */
	@PutMapping
    @ResponseBody
	public ResultMsg update(@RequestBody SysRoleEntity sysRole){
        return sysRoleService.save(sysRole).map(u -> ResultMsg.success()).orElse(ResultMsg.error());
	}
	
	/**
	 * 删除
	 */
	@DeleteMapping("/{roleId}")
    @ResponseBody
	public ResultMsg delete(@PathVariable("roleId") Long[] roleId){
        ResultMsg rm = null;
        try {
			sysRoleService.delete(roleId);
            rm = ResultMsg.success();
        } catch (Exception e) {
            logger.error("",e);
            rm = ResultMsg.error();
        }
        return rm;
	}

//    @PostMapping
//    @ResponseBody
//	public ResultMsg batch(){
//
//        return new ResultMsg();
//    }
//
}
